package com.mfc.education.service.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mfc.education.common.base.result.R;
import com.mfc.education.service.statistics.entity.Daily;
import com.mfc.education.service.statistics.feign.UcenterMemberService;
import com.mfc.education.service.statistics.mapper.DailyMapper;
import com.mfc.education.service.statistics.service.DailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Mfc
 * @since 2021-10-05
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @Override
    public void createStatisticsByDay(String day) {
        //如果当日的统计记录已存在，则删除重新统计|或 提示用户当日记录已存在
        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated", day);
        baseMapper.delete(queryWrapper);


        // 远程获取注册用户数的统计结果
        R r = ucenterMemberService.countRegisterNum(day);
        Integer registerNum = (Integer) r.getData().get("registerNum");


        int loginNum = RandomUtils.nextInt(100, 200);
        int videoViewNum = RandomUtils.nextInt(100, 200);
        int courseNum = RandomUtils.nextInt(100, 200);

        //创建统计数据对象

        Daily daily = new Daily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setCourseNum(courseNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setDateCalculated(day);

        baseMapper.insert(daily);
    }

    @Override
    public Map<String, Map<String, Object>> getChartData(String begin, String end) {
        Map<String, Map<String, Object>> map = new HashMap<>();
        Map<String, Object> registerNum = this.getChartDataByType(begin, end, "register_num");
        Map<String, Object> loginNum = this.getChartDataByType(begin, end, "login_num");
        Map<String, Object> videoViewNum = this.getChartDataByType(begin, end, "video_view_num");
        Map<String, Object> courseNum = this.getChartDataByType(begin, end, "course_num");

        map.put("registerNum", registerNum);
        map.put("loginNum", loginNum);
        map.put("videoViewNum", videoViewNum);
        map.put("courseNum", courseNum);
        return map;

    }


//    @Override
//    public Map<String, Map<String, Object>> getChartData(String begin, String end) {
//        Map<String, Map<String, Object>> map = new HashMap<>();
//        List<Daily> map1 = baseMapper.getChartData(begin, end);
//
//
//        List<Integer> collect = map1.stream().map(Daily::getRegisterNum).collect(Collectors.toList());
//        List<Integer> collect1 = map1.stream().map(Daily::getCourseNum).collect(Collectors.toList());
//        List<Integer> collect2 = map1.stream().map(Daily::getVideoViewNum).collect(Collectors.toList());
//        List<Integer> collect3 = map1.stream().map(Daily::getLoginNum).collect(Collectors.toList());
//        List<String> collect4 = map1.stream().map(Daily::getDateCalculated).collect(Collectors.toList());
//
//        ArrayList<Object> objects = new ArrayList<>();
//        objects.add(collect);
//        objects.add(collect1);
//        objects.add(collect2);
//        objects.add(collect3);
//
//        String[] s = new String[]{"registerNum", "courseNum", "loginNum", "videoViewNum"};
//        for (int i = 0; i < objects.size(); i++) {
//            Map<String, Object> data = new HashMap<>();
//            data.put("xData", objects.get(i));
//            data.put("yData", collect4);
//            map.put(s[i], data);
//
//        }
//
//
//        return map;
//
//    }

    /**
     * 辅助方法：根据类别获取数据
     */
    private Map<String, Object> getChartDataByType(String begin, String end, String type) {

        QueryWrapper<Daily> wrapper = new QueryWrapper<>();

        wrapper.select("date_calculated", type);

        wrapper.between("date_calculated", begin, end);

        List<Map<String, Object>> maps = baseMapper.selectMaps(wrapper);

        List<String> xData = new ArrayList<>();
        List<Integer> yData = new ArrayList<>();


        for (Map<String, Object> map : maps) {
            String dateCalculated = (String) map.get("date_calculated");
            xData.add(dateCalculated);

            Integer num = (Integer) map.get(type);
            yData.add(num);
        }

        Map<String, Object> data = new HashMap<>();

        data.put("xData", xData);
        data.put("yData", yData);


        return data;
    }

}
